import { createRouter, createWebHistory } from 'vue-router'

import index from '../view/index.vue'

const history = createWebHistory()

const routes = [
    { 
        path: '/', 
        component: index,
        redirect: '/',
        children: [
            {
                path: '/',
                name: 'index',
                component: () => import('../view/index/index.vue')
            },
            {
                path: '/addArticle',
                name: 'addArticle',
                component: () => import('../view/acticle/addArticle.vue')
            }
            ,
            {
                path: '/articleList',
                name: 'articleList',
                component: () => import('../view/acticle/articleList.vue')
            },
            {
                path: '/articleType',
                name: 'articleType',
                component: () => import('../view/articleType/articleTypeList.vue')
            }
            ,
            {
                path: '/comment',
                name: 'comment',
                component: () => import('../view/comment/comment.vue')
            },
            {
                path: '/tool',
                name: 'tool',
                component: () => import('../view/tools/tools.vue')
            },
            {
                path: '/setting',
                name: 'setting',
                component: () => import('../view/setting.vue')
            }
        ]
    },{ 
        path: '/login', 
        component: () => import ('../view/login.vue'),
        meta: {
            isToken: true
        }
    },{
        path: '/404',
        component: () => import ('../view/404.vue')
    },
    {
        path: '/:catchAll(.*)',
        redirect: '/404'
    }
]

const router = createRouter({
    history,
    routes
})

router.beforeEach((to, from , next) => {
    if(!to.meta.isToken){
        if(localStorage.getItem('token')){
            next()
        }else{
            console.log('请先登陆'); 
            next({ path: '/login' })
        }
    }else{
        next()
    }
})

export default router